import { createRouter, createWebHashHistory } from "vue-router";
import routes from "./routes";
import { useCachedViewStoreHook } from "./../store/modules/cachedView";
import NProgress from "./../utils/progress";
import setPageTitle from "./../utils/set-page-title";

import { useConfigStoreWithOut } from "@/store/modules/config";

const configStore = useConfigStoreWithOut();

const router = createRouter({
  history: createWebHashHistory(),
  routes
});

router.beforeEach(async (to, from, next) => {
  NProgress.start();
  // 检查 loading状态 如果加载过了 那么直接放过
  if (!configStore.getLoading) {
    // 路由缓存
    useCachedViewStoreHook().addCachedView(to);
    // 页面 title
    setPageTitle(to.meta.title);
    next();
  } else {
    await configStore.I_ACCOUNT();
    // 路由缓存
    useCachedViewStoreHook().addCachedView(to);
    // 页面 title
    setPageTitle(to.meta.title);
    next();
  }
});

router.afterEach(() => {
  NProgress.done();
});

export default router;
